package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if(userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<UserLike> getIlike(Long userId, Integer page, Integer pagesize, boolean b, List<Long> userIds) {
        Criteria criteria = new Criteria();
        if (userIds.size()==0){
            criteria = Criteria.where("userId").is(userId).and("isLike").is(b);
        }else {
            criteria = Criteria.where("userId").is(userId).and("isLike").is(b).and("likeUserId").in(userIds);
        }

        Query query = new Query(criteria);
        long count = mongoTemplate.count(query, UserLike.class);
        if (count<(page-1)*pagesize) {
            if (count==0){
                return new ArrayList<UserLike>();
            }else {
                page = Integer.valueOf(String.valueOf(count))/pagesize;
            }
        }
        query.skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        System.out.println(userLikes);
        return userLikes;
    }

    @Override
    public Long getCount(Long userId, Boolean b, Integer type) {

        if (type == 2){
            //查询 我关注的总数
            Criteria criteria = Criteria.where("userId").is(userId).and("isLike").is(b);
            Query query = new Query(criteria);

            return mongoTemplate.count(query,UserLike.class);
        }else {
            //查询关注我的总数
            Criteria criteria1=Criteria.where("likeUserId").is(userId).and("isLike").is(b);
            Query query = new Query(criteria1);

            return mongoTemplate.count(query,UserLike.class);
        }

    }

    @Override
    public List<UserLike> getLikeme(Long userId, Integer page, Integer pagesize, boolean b, List<Long> userIds) {
        Criteria criteria = new Criteria();

        if (userIds.size()==0){
            criteria = Criteria.where("likeUserId").is(userId).and("isLike").is(b);
        }else {
            criteria = Criteria.where("likeUserId").is(userId).and("userId").in(userIds).and("isLike").is(b);
        }
        Query query = new Query(criteria);
        long count = mongoTemplate.count(query, UserLike.class);
        if (count<(page-1)*pagesize) {
            if (count==0){
                return new ArrayList<UserLike>();
            }else {
                page = Integer.valueOf(String.valueOf(count))/pagesize;
            }
        }
        query.skip((page-1)*pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query,UserLike.class);
    }


    @Override
    public Boolean remove(Long userId, Long id) {
        Criteria criteria = Criteria.where("likeUserId").is(id).and("userId").is(userId).and("isLike").is(true);
        Query query = new Query(criteria);
        mongoTemplate.remove(query,UserLike.class);

        return true;
    }

    @Override
    public PageResult findPageByUserId(Integer page, Integer pagesize, Long userId) {
        //1.分页查询喜欢数据
        Query query=Query.query(Criteria.where("userId").is(userId)).skip((page-1)*pagesize).limit(pagesize);
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        //2.查询
        long count = mongoTemplate.count(query, UserLike.class);
        PageResult pageResult=new PageResult(page,pagesize, (long)count,userLikes);
        return pageResult;
    }

}
